home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_096 / animplayer / readpict.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  158 lines

  1. /** ReadPict.c **************************************************************
  2.  *
  3.  * Read an ILBM raster image file.   01/07/85.
  4.  *
  5.  * By Jerry Morrison, Steve Shaw, and Steve Hayes, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * USE THIS AS AN EXAMPLE PROGRAM FOR AN IFF READER.
  9.  *
  10.  * The IFF reader portion is essentially a recursive-descent parser.
  11.  * This program will look into a CAT or LIST to find a FORM ILBM, but it
  12.  * won't look inside another FORM type for a nested FORM ILBM.
  13.  *
  14.  * Modified for HAM images by Martin Hash.    12/20/86.
  15.  *
  16.  ****************************************************************************/
  17.  
  18. #include <libraries/dos.h>
  19. #include <libraries/dosextens.h>
  20. #include "df1:ilbm.h"
  21. #include "df1:readpict.h"
  22.  
  23. /* LOCAL CONSTANTS */
  24.  
  25. /* Define the size of a temporary buffer used in unscrambling the ILBM rows.*/
  26. #define bufSz         512
  27. #define maxColorReg    32
  28.  
  29. /* EXTERNAL FUNCTIONS */
  30.  
  31. extern IFFP OpenRGroup();
  32. extern IFFP GetFChunkHdr();
  33.  
  34. /* LOCAL VARIABLES */
  35.  
  36. static ILBMFrame iFrame;
  37. static BYTE bodyBuffer[bufSz];
  38. static struct BitMap *bitmapptr;
  39.  
  40. /*------------ ILBM reader -----------------------------------------------*/
  41. /* ILBMFrame is our "client frame" for reading FORMs ILBM in an IFF file.
  42.  * We allocate one of these on the stack for every LIST or FORM encountered
  43.  * in the file and use it to hold BMHD & CMAP properties. We also allocate
  44.  * an initial one for the whole file.
  45.  * We allocate a new GroupContext (and initialize it by OpenRIFF or
  46.  * OpenRGroup) for every group (FORM, CAT, LIST, or PROP) encountered. It's
  47.  * just a context for reading (nested) chunks.
  48.  *
  49.  * If we were to scan the entire example file outlined below:
  50.  *    reading          proc(s)                new               new
  51.  *
  52.  * --whole file--   ReadPicture+ReadIFF   GroupContext        ILBMFrame
  53.  * CAT              ReadICat                GroupContext
  54.  *   LIST           GetLiILBM+ReadIList       GroupContext        ILBMFrame
  55.  *     PROP ILBM    GetPrILBM                   GroupContext
  56.  *       CMAP       GetCMAP
  57.  *       BMHD       GetBMHD
  58.  *     FORM ILBM    GetFoILBM                   GroupContext        ILBMFrame
  59.  *       BODY       GetBODY
  60.  *     FORM ILBM    GetFoILBM                   GroupContext        ILBMFrame
  61.  *       BODY       GetBODY
  62.  *   FORM ILBM      GetFoILBM                 GroupContext        ILBMFrame
  63.  */
  64.  
  65. /** GetFoILBM() *************************************************************
  66.  *
  67.  * Called via ReadPicture to handle every FORM encountered in an IFF file.
  68.  * Reads FORMs ILBM and skips all others.
  69.  * Inside a FORM ILBM, it stops once it reads a BODY. It complains if it
  70.  * finds no BODY or if it has no BMHD to decode the BODY.
  71.  *
  72.  ****************************************************************************/
  73.  
  74. IFFP GetFoILBM( parent )
  75.  
  76. GroupContext *parent;
  77. {
  78.    IFFP iffp;
  79.    GroupContext formContext;
  80.    ILBMFrame ilbmFrame;         /* only used for non-clientFrame fields.*/
  81.  
  82.    if (parent->subtype != ID_ILBM)
  83.       return(IFF_OKAY); /* just continue scaning the file */
  84.  
  85.    ilbmFrame = *(ILBMFrame *)parent->clientFrame;
  86.    iffp = OpenRGroup(parent, &formContext);
  87.    CheckIFFP();
  88.  
  89.   do {
  90.     iffp = GetFChunkHdr(&formContext);
  91.     if (iffp == ID_BMHD) {
  92.        ilbmFrame.foundBMHD = TRUE;
  93.        iffp = GetBMHD(&formContext, &ilbmFrame.bmHdr);
  94.     }
  95.     else if (iffp == ID_CMAP) { 
  96.        ilbmFrame.nColorRegs = maxColorReg;
  97.        iffp = GetCMAP( &formContext, (WORD *)ilbmFrame.colorMap,
  98.         &ilbmFrame.nColorRegs);
  99.     }
  100.     else if (iffp == ID_BODY) {
  101.        if (!ilbmFrame.foundBMHD)  
  102.           return(BAD_FORM);   /* No BMHD chunk! */
  103.  
  104.        iffp = GetBODY( &formContext, bitmapptr, NULL, &ilbmFrame.bmHdr,
  105.         bodyBuffer, (long)bufSz);
  106.  
  107.        if (iffp == IFF_OKAY)
  108.           iffp = IFF_DONE;      /* Eureka */
  109.        iFrame = ilbmFrame;
  110.     }
  111.  
  112.     else if (iffp == END_MARK)
  113.        iffp = BAD_FORM;
  114.  
  115.   } while (iffp >= IFF_OKAY);  /* loop if valid ID of ignored chunk or a
  116.                           * subroutine returned IFF_OKAY (no errors).*/
  117.  
  118.    if (iffp != IFF_DONE)  return(iffp);
  119.  
  120.    /* If we get this far, there were no errors. */
  121.    CloseRGroup(&formContext);
  122.    return(iffp);
  123. }
  124.  
  125. /** ReadPicture() ***********************************************************
  126.  *
  127.  * Read a picture from an IFF file, given a file handle open for reading.
  128.  * Allocates BitMap using (*Allocator)().
  129.  *
  130.  ****************************************************************************/
  131. IFFP ReadPicture( file, bitmap, viewport )
  132.  
  133. LONG file;
  134. struct BitMap *bitmap;
  135. struct ViewPort *viewport;
  136. {
  137.    IFFP iffp;
  138.  
  139.    iFrame.clientFrame.getList = SkipGroup;
  140.    iFrame.clientFrame.getProp = SkipGroup;
  141.    iFrame.clientFrame.getForm = GetFoILBM;
  142.    iFrame.clientFrame.getCat  = ReadICat ;
  143.  
  144.    /* Initialize the top-level client frame's property settings to the
  145.     * program-wide defaults. This example just records that we haven't read
  146.     * any BMHD property or CMAP color registers yet. For the color map, that
  147.     * means the default is to leave the machine's color registers alone.
  148.     * If you want to read a property like GRAB, init it here to (0, 0). */
  149.    iFrame.foundBMHD  = FALSE;
  150.    iFrame.nColorRegs = 0;
  151.  
  152.    bitmapptr = bitmap;
  153.  
  154.    iffp = ReadIFF(file, (ClientFrame *)&iFrame);
  155.    LoadRGB4( viewport, iFrame.colorMap, iFrame.nColorRegs );
  156.    return(iffp);
  157. }
  158.